﻿using System;

class ArrayBinSearch
{
    static void Main()
    {
        Console.Write("Enter the length of the array: ");

        int n = int.Parse(Console.ReadLine());
        int[] array = new int[n];

        Console.WriteLine("Enter values for the array elements:");

        for (int i = 0; i < n; i++)
        {
            Console.Write("[{0}] = ", i);
            array[i] = int.Parse(Console.ReadLine());
        }

        Console.Write("Enter an integer number K: ");

        int k = int.Parse(Console.ReadLine());
        int index = -1;

        Array.Sort(array);

        index = Array.BinarySearch(array, k);

        if (index >= 0)
        {
            Console.WriteLine("The largest number in the array which is <= K is:");
            Console.WriteLine(array[index]);
        }
        else if (index < -1)
        {
            Console.WriteLine("The largest number in the array which is <= K is:");
            Console.WriteLine(array[~index - 1]);
        }
        else
        {
            Console.WriteLine("All numbers in the array are bigger than K!");
        }
    }
}
